<?php

namespace App\Http\Controllers\Backend\System;

use App\Http\Controllers\BaseController;
use App\Http\Controllers\Controller;
use App\Interfaces\RoleInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class RoleController extends BaseController
{
    protected RoleInterface $role;

    public function __construct(RoleInterface $role)
    {
        $this->role = $role;
    }

    /**
     * 获取所有目录
     * @return JsonResponse
     */
    public function getRoleAll(): JsonResponse
    {
        $roles = $this->role->getAllRoles();
        return $this->apiResult(['code'=>0,'data'=>$roles,'message'=>'success']);
    }

    /**
     * 分页获取目录
     * @param Request $request
     * @return JsonResponse
     */
    public function getRolePage(Request $request): JsonResponse
    {
        $params['roleName'] = $request->input('roleName');
        $params['roleCode'] = $request->input('roleCode');
        $params['comments'] = $request->input('comments');
        $params['sort'] = $request->input('sort');
        $params['order'] = $request->input('order');
        $page = $request->input('page');
        $limit = $request->input('limit');
        $roles = $this->role->getRolePage($params,$page,$limit);
        return $this->apiResult(['code'=>0,'data'=>$roles,'message'=>'success']);
    }

    /**
     * 管理角色目录权限
     * @param Request $request
     * @param int $roleId
     * @return JsonResponse
     */
    public function updateRoleMenu(Request $request, int $roleId): JsonResponse
    {
        $menuId = $request->input();
        $data = array('menuId'=>$menuId);
        $res = $this->role->updateRoleById($roleId,$data);
        if (!$res)
        {
            return $this->apiResult(['code'=>1, 'message'=>'Fail']);
        }
        return $this->apiResult(['code'=>0, 'message'=>'success']);
    }

    /**
     * 创建角色
     * @param Request $request
     * @return JsonResponse
     */
    public function createRole(Request $request): JsonResponse
    {
        $data = $request->input();
        $data['createTime'] = dateTime();

        $res = $this->role->createRole($data);
        if (!$res){
            return $this->apiResult(['code'=>1,'message'=>'创建角色失败！']);
        }
        return $this->apiResult(['code'=>0,'message'=>'创建角色成功！']);
    }

    /**
     * 删除角色
     * @param $roleId
     * @return JsonResponse
     */
    public function deleteRole($roleId): JsonResponse
    {
        $data['deleted'] = 1;
        $data['updateTime'] = dateTime();

        $res = $this->role->updateRoleById($roleId,$data);
        if(!$res){
            return $this->apiResult(['code'=>1,'message'=>'删除角色失败！']);
        }
        return $this->apiResult(['code'=>0,'message'=>'删除角色成功！']);
    }

    /**
     * 批量删除角色
     * @param Request $request
     * @return JsonResponse
     */
    public function deleteRoleBatch(Request $request): JsonResponse
    {
        $roleIds = $request->input();

        $data['deleted'] = 1;
        $data['updateTime'] = dateTime();

        $res = $this->role->updateBatch($roleIds,$data);
        if(!$res){
            return $this->apiResult(['code'=>1,'message'=>'批量删除角色失败！']);
        }
        return $this->apiResult(['code'=>0,'message'=>'批量删除角色成功！']);
    }

    /**
     * 修改角色
     * @param Request $request
     * @return JsonResponse
     */
    public function updateRole(Request $request): JsonResponse
    {
        $data = $request->input();
        $data['updateTime'] = dateTime();
        $res = $this->role->updateRoleById($data['roleId'],$data);
        if(!$res)
        {
            return $this->apiResult(['code'=>1,'message'=>'修改角色失败！']);
        }
        return $this->apiResult(['code'=>0,'message'=>'修改角色'.$data['roleId'].'成功！']);
    }
    }

